package de.swta.freezay.admin.unit;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.controller.exceptions.NonexistentEntityException;
import de.swta.freezay.database.controller.exceptions.PreexistingEntityException;
import de.swta.freezay.database.dbEntities.Unit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

/**
 *
 * @author Jan
 */
public abstract class UnitEditPanel extends Panel {

    /* Form to edit a unit. */
    Form unitForm;
    /** EditMode. If unit is edited editMode is true. */
    boolean editMode;

    /**
     * Creates a new instance of UnitEditPanel
     * @param id Id of the panel.
     * @param unitId Id of the unit.
     * @param editMode If unit is edited editMode is true.
     */
    public UnitEditPanel(String id, int unitId, boolean editMode) {
        super(id);
        this.editMode = editMode;
        init(unitId);
    }

    /**
     * Initializes the components of UnitEditPanel.
     *
     * @param unitId Id of the unit.
     */
    private void init(int unitId) {

        Unit u;
        if (editMode) {
            /* if edit => find it. */
            u = JPAController.UnitJpaController.findUnit(unitId);
        } else {
            /* otherwise create it. */
            u = new Unit();
        }

        unitForm = new Form("unitForm");

        unitForm.add(new AjaxSubmitLink("saveLink") {

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                Unit u = (Unit) unitForm.getModelObject();

                if (editMode) {
                    try {
                        /* edit the unit. */
                        JPAController.UnitJpaController.edit(u);
                    } catch (NonexistentEntityException ex) {
                        Logger.getLogger(UnitEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (Exception ex) {
                        Logger.getLogger(UnitEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    try {
                        /* create it. */
                        JPAController.UnitJpaController.create(u);
                    } catch (PreexistingEntityException ex) {
                        Logger.getLogger(UnitEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (Exception ex) {
                        Logger.getLogger(UnitEditPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                UnitEditPanel.this.saveLinkClicked(target);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(this.getParent().get("feedback"));
                super.onError(target, form);
            }
        });

        unitForm.setModel(new CompoundPropertyModel(u));
        unitForm.add(new FeedbackPanel("feedback").setOutputMarkupId(true));
        unitForm.add(new RequiredTextField("name"));
        add(unitForm);
    }

    /**
     * Handles the actions after the save link was clicked.
     *
     * @param target AjaxRequestTarget.
     */
    public abstract void saveLinkClicked(AjaxRequestTarget target);
}
